<?php
class ProjectExportController extends Zone_Action{
	public function indexAction(){
		$this->removeLayout();
		$user_id = get_user_id();
		$ids = get('ID',array(),true);

		if( count($ids) == 0 ){
			return $this->setContent("<script> parent.$.Alert('Bạn chưa chọn dự án nào'); </script>");
		}else{
			foreach( $ids as $j => $v ){
				$ids[$j] = "'$v'";
			}
			
			$posts = $this->Model->fetchAll("SELECT `a`.* 			
				FROM  `projects` as `a`
				LEFT JOIN `projects_users` as `c`
					ON `a`.`ID`=`c`.`project_id` 
						AND `c`.`user_id`='$user_id'
				WHERE `a`.`is_draft`='no' AND ( `c`.`user_id` IS NOT NULL OR `a`.`created_by_id`='$user_id' ) AND `a`.`ID` IN (".implode(',',$ids).")");		
		}
		
	header("Content-type: application/excel; charset=utf8");
    header('Content-disposition: attachment; filename="project-export-'.$user_id.'-'.mysql_date().'.xls"');
	
	echo '<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Version>12.00</Version>
 </DocumentProperties>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>8505</WindowHeight>
  <WindowWidth>19155</WindowWidth>
  <WindowTopX>0</WindowTopX>
  <WindowTopY>30</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Arial" x:Family="Swiss"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s62" ss:Name="Normal 3">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font ss:FontName="Arial" x:Family="Swiss"/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
  <Style ss:ID="s64" ss:Parent="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
   <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="12"/>
   <Interior/>
   <NumberFormat/>
  </Style>
  <Style ss:ID="s66">
   <Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
   <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="16" ss:Bold="1"/>
   <Interior/>
  </Style>
  <Style ss:ID="s67" ss:Parent="s62">
   <Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
   <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="12"/>
   <Interior/>
  </Style>
  <Style ss:ID="s68">
   <Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
   <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="13" ss:Bold="1"/>
   <Interior/>
  </Style>
  <Style ss:ID="s69">
   <Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:ShrinkToFit="1"/>
   <Font ss:FontName="Times New Roman" x:Family="Roman" ss:Size="13" ss:Bold="1"/>
   <Interior/>
  </Style>
  <Style ss:ID="s71" ss:Parent="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:ShrinkToFit="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#333333"
    ss:Bold="1"/>
   <Interior ss:Color="#D8D8D8" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="s72">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:ShrinkToFit="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#333333"
    ss:Bold="1"/>
   <Interior ss:Color="#D8D8D8" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="s73">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
  </Style>
  <Style ss:ID="s74" ss:Parent="s62">
   <Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:WrapText="1"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#333333"
    ss:Bold="1"/>
   <Interior ss:Color="#D8D8D8" ss:Pattern="Solid"/>
  </Style>
  <Style ss:ID="s75">
   <Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
   <Borders>
    <Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1"/>
    <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1"/>
   </Borders>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
  </Style>
 </Styles>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="11" ss:ExpandedRowCount="13" x:FullColumns="1"
   x:FullRows="1" ss:DefaultRowHeight="15">
   <Column ss:AutoFitWidth="0" ss:Width="28.5"/>
   <Column ss:AutoFitWidth="0" ss:Width="84"/>
   <Column ss:AutoFitWidth="0" ss:Width="168.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="100.5"/>
   <Column ss:AutoFitWidth="0" ss:Width="89.25"/>
   <Column ss:AutoFitWidth="0" ss:Width="87"/>
   <Column ss:AutoFitWidth="0" ss:Width="76.5"/>
   <Column ss:Width="123.75"/>
   <Column ss:AutoFitWidth="0" ss:Width="68.25"/>
   <Column ss:Index="11" ss:AutoFitWidth="0" ss:Width="64.5"/>
   <Row ss:AutoFitHeight="0" ss:Height="21.75">
    <Cell ss:StyleID="s64"/>
    <Cell ss:MergeAcross="3" ss:StyleID="s66"><Data ss:Type="String">       DANH SÁCH DỰ ÁN</Data></Cell>
    <Cell ss:StyleID="s67"/>
    <Cell ss:StyleID="s67"/>
    <Cell ss:StyleID="s67"/>
    <Cell ss:StyleID="s67"/>
    <Cell ss:StyleID="s67"/>
    <Cell ss:StyleID="s67"/>
   </Row>
   <Row ss:AutoFitHeight="0" ss:Height="16.5">
    <Cell ss:StyleID="s68"/>
    <Cell ss:StyleID="s68"/>
    <Cell ss:StyleID="s68"/>
    <Cell ss:StyleID="s69"/>
    <Cell ss:StyleID="s69"/>
    <Cell ss:StyleID="s69"/>
    <Cell ss:StyleID="s69"/>
    <Cell ss:StyleID="s69"/>
   </Row>
   <Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s74"><Data ss:Type="String">TT</Data></Cell>
    <Cell ss:StyleID="s74"><Data ss:Type="String">Mã dự án</Data></Cell>
    <Cell ss:StyleID="s74"><Data ss:Type="String">Tên dự án</Data></Cell>
    <Cell ss:StyleID="s71"><Data ss:Type="String">Bắt đầu</Data></Cell>
    <Cell ss:StyleID="s71"><Data ss:Type="String">Dự kiến</Data></Cell>
    <Cell ss:StyleID="s72"><Data ss:Type="String">Kết thúc</Data></Cell>
    <Cell ss:StyleID="s72"><Data ss:Type="String">Tiến độ</Data></Cell>
    <Cell ss:StyleID="s72"><Data ss:Type="String">Phụ trách</Data></Cell>
   </Row>';
 
  foreach( $posts as  $k => $post ){
	    $post['status'] = $post['status'] == "active" ? "Thực hiện" :
			( $post['status']=="inactive" ? "Tạm dừng" : "Kết thúc" );
	   
        $users = $this
			->Model->fetchAll("SELECT `b`.*,`a`.*
				FROM `users` as `a` 
				LEFT JOIN `projects_users` as `b`
					ON `b`.`user_id`=`a`.`ID` AND `b`.`project_id`='{$post['ID']}'
				WHERE
					`b`.`project_id`='{$post['ID']}' AND `b`.`is_supervisor`='yes'");			
	
		foreach( $users as $user ){
			$post['supervisors'][] = $user['username'];	
		}		
	   
	   echo '<Row ss:AutoFitHeight="0">
    <Cell ss:StyleID="s73"><Data ss:Type="Number">'.($k+1).'</Data></Cell>
    <Cell ss:StyleID="s73"><Data ss:Type="String">'.$post['code'].'</Data></Cell>
    <Cell ss:StyleID="s75"><Data ss:Type="String">'.$post['title'].'</Data></Cell>
    <Cell ss:StyleID="s73"><Data ss:Type="String">'.show_date(DATE_FORMAT,$post['date_start']).'</Data></Cell>
    <Cell ss:StyleID="s73"><Data ss:Type="String">'.show_date(DATE_FORMAT,$post['date_end']).'</Data></Cell>
    <Cell ss:StyleID="s73"><Data ss:Type="String">'.show_date(DATE_FORMAT,$post['date_end_forecast']).'</Data></Cell>
    <Cell ss:StyleID="s73"><Data ss:Type="String">'.$post['rate'].' %</Data></Cell>
    <Cell ss:StyleID="s75"><Data ss:Type="String">'.implode(', ',$post['supervisors']).'</Data></Cell>
   </Row>';
	}
  echo '</Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <PageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <Unsynced/>
   <Print>
    <ValidPrinterInfo/>
    <HorizontalResolution>300</HorizontalResolution>
    <VerticalResolution>300</VerticalResolution>
   </Print>
   <Selected/>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
 </Worksheet>
</Workbook>';
	exit;				
}}	